<template>
  <RouterView v-wechat-title="route.meta.title" />
</template>

<script setup>
import { RouterView, useRoute } from "vue-router";
import { GetInfoByCode } from "@/api/index.js";
import { getCode } from "@/api/getDDcode.js";
import { Local } from "@/utils/storage";
import { onMounted } from "vue";
import { useUserStore } from "./stores/user";
import { ref, nextTick } from "vue";

const userStore = useUserStore();

const route = useRoute();
// 根据code 获取用户信息
const getUserInfoByCode = async () => {
  await getCode(async (code) => {
    console.log("codeapp", code);
    if (!code) return;
    let result = await GetInfoByCode({ code });
    Local.set("userName", result.data.userName);
    Local.set("userId", result.data.userId);
    Local.set("userImg", result.data.img);
    useUserStore().setUserId(result.data.userId);
  });
};
// 判断是电脑还是手机
const isMobile = ref(null);
// 判断是否是手机端，如果是，返回true
const _isMobile = () => {
  nextTick(() => {
    isMobile.value = navigator.userAgent.match(
      /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
    );
    localStorage.setItem(
      "isMobile",
      JSON.parse(JSON.stringify(isMobile.value)) ? true : false
    );
  });
};
_isMobile();

onMounted(() => {
  getUserInfoByCode();
});
</script>
